package cn.cena.esdome;

import org.apache.lucene.util.QueryBuilder;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ESTest {

    public TransportClient getClient(){
        Settings settings = Settings.builder()
                .put("client.transport.sniff", true).build();
        TransportClient client = null;
        try {
            client = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        return client;
    }



    @Test
    public void testAdd() {
        // 获取连接
        TransportClient client = getClient();

        // 创建索引
        IndexRequestBuilder indexRequestBuilder = client.prepareIndex("pethome", "user", "1");

       Map<String, Object> map = new HashMap<String, Object>();
       map.put("id", 1L);
       map.put("username", "hh");
       map.put("realName", "花花");
       map.put("age", 20);
       map.put("sex", 1);
       // 添加文档
        IndexResponse response = indexRequestBuilder.setSource(map).get();
        System.out.println(response);

    }

    @Test
    public void name() {
        TransportClient client = getClient();
        Map<String, Object> source = client.prepareGet("pethome", "user", "1").get().getSource();
        System.out.println(source);
    }

    @Test
    public void update() {
        TransportClient client = getClient();
        UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate("pethome", "user", "1");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", 1L);
        map.put("username", "hh");
        map.put("realName", "阿牛");
        map.put("age", 20);
        map.put("sex", 1);
        UpdateResponse updateResponse = updateRequestBuilder.setDoc(map).get();
        System.out.println(updateResponse);
    }

    @Test
    public void delete() {
        TransportClient client = getClient();
        DeleteResponse deleteResponse = client.prepareDelete("pethome", "user", "1").get();
        System.out.println(deleteResponse);
    }

    @Test
    public void bulk() {
        TransportClient client = getClient();
        BulkRequestBuilder bulk = client.prepareBulk();

        // 批量添加
        for (int i=0; i<50; i++){
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", i);
            map.put("username", i%2 == 0?"hh":"bb");
            map.put("realName", i%2 == 0?"花花":"阿牛");
            map.put("age", i);
            map.put("sex", i%2 == 0?"1":"0");
            bulk.add(client.prepareIndex("pethome", "user",""+i).setSource(map));
        }

        BulkResponse bulkItemResponses = bulk.get();
        System.out.println(bulkItemResponses);
    }

    @Test
    public void search() {
        TransportClient client = getClient();
        SearchRequestBuilder pethome = client.prepareSearch("pethome");
        // 排序
        pethome.addSort("age", SortOrder.DESC);
        // 分页
        pethome.setFrom(0);
        pethome.setSize(5);

        // 查询条件
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.matchQuery("username", "hh"));
        // 过滤条件
        boolQueryBuilder.filter(QueryBuilders.termQuery("sex", 1))
                        .filter(QueryBuilders.rangeQuery("age").gte(10).lte(30));
        SearchResponse searchResponse = pethome.setQuery(boolQueryBuilder).get();

        // 查询到的结果
        SearchHits hits = searchResponse.getHits();

        // 查询到的条数
        long totalHits = hits.getTotalHits();
        System.out.println("总条数：" + totalHits);

        // 结果列表
        SearchHit[] hits1 = hits.getHits();
        for (SearchHit searchHit : hits1){
            Map<String, Object> source = searchHit.getSource();
            System.out.println(source);
        }

    }
}
